package com.youyi.yyviewsdklibrary.Dialog.core;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ArgbEvaluator;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.transition.ChangeBounds;
import android.transition.ChangeImageTransform;
import android.transition.ChangeTransform;
import android.transition.Transition;
import android.transition.TransitionListenerAdapter;
import android.transition.TransitionManager;
import android.transition.TransitionSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.youyi.yyviewsdklibrary.Dialog.enums.PopupStatus;
import com.youyi.yyviewsdklibrary.Dialog.interfaces.OnDragChangeListener;
import com.youyi.yyviewsdklibrary.Dialog.interfaces.OnImageViewerLongPressListener;
import com.youyi.yyviewsdklibrary.Dialog.interfaces.OnSrcViewUpdateListener;
import com.youyi.yyviewsdklibrary.Dialog.interfaces.XPopupImageLoader;
import com.youyi.yyviewsdklibrary.Dialog.photoview.PhotoView;
import com.youyi.yyviewsdklibrary.Dialog.util.PermissionConstants;
import com.youyi.yyviewsdklibrary.Dialog.util.XPermission;
import com.youyi.yyviewsdklibrary.Dialog.util.XPopupUtils;
import com.youyi.yyviewsdklibrary.Dialog.widget.BlankView;
import com.youyi.yyviewsdklibrary.Dialog.widget.HackyViewPager;
import com.youyi.yyviewsdklibrary.Dialog.widget.PhotoViewContainer;
import com.lkx.liandianqinew.R;
import java.util.ArrayList;
import java.util.List;

public class ImageViewerPopupView extends BasePopupView implements OnDragChangeListener, View.OnClickListener {
    protected ArgbEvaluator argbEvaluator = new ArgbEvaluator();
    protected int bgColor = Color.rgb(32, 36, 46);
    protected FrameLayout container = ((FrameLayout) findViewById(R.id.container));
    protected View customView;
    protected XPopupImageLoader imageLoader;
    protected ImageView img_close;
    protected ImageView img_save;
    protected boolean isInfinite = false;
    protected boolean isShowIndicator = true;
    protected boolean isShowPlaceholder = true;
    protected boolean isShowSaveBtn = true;
    public OnImageViewerLongPressListener longPressListener;
    protected HackyViewPager pager;
    protected PhotoViewContainer photoViewContainer;
    protected int placeholderColor = Color.parseColor("#f1f1f1");
    protected int placeholderRadius = -1;
    protected int placeholderStrokeColor = -1;
    protected BlankView placeholderView;
    protected int position;
    protected Rect rect = null;
    protected PhotoView snapshotView;
    protected ImageView srcView;
    protected OnSrcViewUpdateListener srcViewUpdateListener;
    protected TextView tv_pager_indicator;
    protected List<Object> urls = new ArrayList();

    public ImageViewerPopupView(Context context) {
        super(context);
        if (getImplLayoutId() > 0) {
            View inflate = LayoutInflater.from(getContext()).inflate(getImplLayoutId(), (ViewGroup) this.container, false);
            this.customView = inflate;
            inflate.setVisibility(View.INVISIBLE);
            this.customView.setAlpha(0.0f);
            this.container.addView(this.customView);
        }
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public final int getInnerLayoutId() {
        return R.layout._xpopup_image_viewer_popup_view;
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public void initPopupContent() {
        super.initPopupContent();
        this.tv_pager_indicator = (TextView) findViewById(R.id.tv_pager_indicator);
        this.img_close = (ImageView) findViewById(R.id.id_close);
        this.img_save = (ImageView) findViewById(R.id.id_save);
        this.placeholderView = (BlankView) findViewById(R.id.placeholderView);
        PhotoViewContainer photoViewContainer2 = (PhotoViewContainer) findViewById(R.id.photoViewContainer);
        this.photoViewContainer = photoViewContainer2;
        photoViewContainer2.setOnDragChangeListener(this);
        this.pager = (HackyViewPager) findViewById(R.id.pager);
        PhotoViewAdapter photoViewAdapter = new PhotoViewAdapter();
        this.pager.setAdapter(photoViewAdapter);
        this.pager.setCurrentItem(this.position);
        this.pager.setVisibility(View.INVISIBLE);
        addOrUpdateSnapshot();
        this.pager.setOffscreenPageLimit(2);
        this.pager.addOnPageChangeListener(photoViewAdapter);
        if (!this.isShowIndicator) {
            this.tv_pager_indicator.setVisibility(View.GONE);
        }
        this.img_close.setOnClickListener(this);
        if (!this.isShowSaveBtn) {
            this.img_save.setVisibility(View.GONE);
        } else {
            this.img_save.setOnClickListener(this);
        }
    }

    private void setupPlaceholder() {
        this.placeholderView.setVisibility(this.isShowPlaceholder ? 0 : 4);
        if (this.isShowPlaceholder) {
            int i = this.placeholderColor;
            if (i != -1) {
                this.placeholderView.color = i;
            }
            int i2 = this.placeholderRadius;
            if (i2 != -1) {
                this.placeholderView.radius = i2;
            }
            int i3 = this.placeholderStrokeColor;
            if (i3 != -1) {
                this.placeholderView.strokeColor = i3;
            }
            XPopupUtils.setWidthHeight(this.placeholderView, this.rect.width(), this.rect.height());
            this.placeholderView.setTranslationX((float) this.rect.left);
            this.placeholderView.setTranslationY((float) this.rect.top);
            this.placeholderView.invalidate();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showPagerIndicator() {
        if (this.urls.size() > 1) {
            int realPosition = getRealPosition();
            TextView textView = this.tv_pager_indicator;
            textView.setText((realPosition + 1) + "/" + this.urls.size());
        }
        if (this.isShowSaveBtn) {
            this.img_save.setVisibility(View.VISIBLE);
        }
    }

    private void addOrUpdateSnapshot() {
        if (this.srcView != null) {
            if (this.snapshotView == null) {
                PhotoView photoView = new PhotoView(getContext());
                this.snapshotView = photoView;
                photoView.setEnabled(false);
                this.photoViewContainer.addView(this.snapshotView);
                this.snapshotView.setScaleType(this.srcView.getScaleType());
                this.snapshotView.setTranslationX((float) this.rect.left);
                this.snapshotView.setTranslationY((float) this.rect.top);
                XPopupUtils.setWidthHeight(this.snapshotView, this.rect.width(), this.rect.height());
            }
            int realPosition = getRealPosition();
            this.snapshotView.setTag(Integer.valueOf(realPosition));
            setupPlaceholder();
            XPopupImageLoader xPopupImageLoader = this.imageLoader;
            if (xPopupImageLoader != null) {
                xPopupImageLoader.loadSnapshot(this.urls.get(realPosition), this.snapshotView, this.srcView);
            }
        }
    }

    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public void doShowAnimation() {
        if (this.srcView == null) {
            this.photoViewContainer.setBackgroundColor(this.bgColor);
            this.pager.setVisibility(View.VISIBLE);
            showPagerIndicator();
            this.photoViewContainer.isReleasing = false;
            doAfterShow();
            View view = this.customView;
            if (view != null) {
                view.setAlpha(1.0f);
                this.customView.setVisibility(View.VISIBLE);
                return;
            }
            return;
        }
        this.photoViewContainer.isReleasing = true;
        View view2 = this.customView;
        if (view2 != null) {
            view2.setVisibility(View.VISIBLE);
        }
        this.snapshotView.setVisibility(View.VISIBLE);
        doAfterShow();
        this.snapshotView.post(new Runnable() {
            /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass1 */

            public void run() {
                if (Build.VERSION.SDK_INT >= 26) {
                    TransitionManager.beginDelayedTransition((ViewGroup) ImageViewerPopupView.this.snapshotView.getParent(), new TransitionSet().setDuration((long) ImageViewerPopupView.this.getAnimationDuration()).addTransition(new ChangeBounds()).addTransition(new ChangeTransform()).addTransition(new ChangeImageTransform()).setInterpolator((TimeInterpolator) new FastOutSlowInInterpolator()).addListener((Transition.TransitionListener) new TransitionListenerAdapter() {
                        /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass1.AnonymousClass1 */

                        public void onTransitionEnd(Transition transition) {
                            ImageViewerPopupView.this.pager.setVisibility(View.VISIBLE);
                            ImageViewerPopupView.this.snapshotView.setVisibility(View.INVISIBLE);
                            ImageViewerPopupView.this.showPagerIndicator();
                            ImageViewerPopupView.this.photoViewContainer.isReleasing = false;
                        }
                    }));
                }
                ImageViewerPopupView.this.snapshotView.setTranslationY(0.0f);
                ImageViewerPopupView.this.snapshotView.setTranslationX(0.0f);
                ImageViewerPopupView.this.snapshotView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                XPopupUtils.setWidthHeight(ImageViewerPopupView.this.snapshotView, ImageViewerPopupView.this.photoViewContainer.getWidth(), ImageViewerPopupView.this.photoViewContainer.getHeight());
                ImageViewerPopupView imageViewerPopupView = ImageViewerPopupView.this;
                imageViewerPopupView.animateShadowBg(imageViewerPopupView.bgColor);
                if (ImageViewerPopupView.this.customView != null) {
                    ImageViewerPopupView.this.customView.animate().alpha(1.0f).setDuration((long) ImageViewerPopupView.this.getAnimationDuration()).start();
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void animateShadowBg(final int i) {
        final int color = ((ColorDrawable) this.photoViewContainer.getBackground()).getColor();
        ValueAnimator ofFloat = ValueAnimator.ofFloat(0.0f, 1.0f);
        ofFloat.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass2 */

            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                ImageViewerPopupView.this.photoViewContainer.setBackgroundColor(((Integer) ImageViewerPopupView.this.argbEvaluator.evaluate(valueAnimator.getAnimatedFraction(), Integer.valueOf(color), Integer.valueOf(i))).intValue());
            }
        });
        ofFloat.setDuration((long) getAnimationDuration()).setInterpolator(new LinearInterpolator());
        ofFloat.start();
    }

    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public void doDismissAnimation() {
        if (this.srcView == null) {
            this.photoViewContainer.setBackgroundColor(0);
            doAfterDismiss();
            this.pager.setVisibility(View.INVISIBLE);
            this.placeholderView.setVisibility(View.INVISIBLE);
            View view = this.customView;
            if (view != null) {
                view.setAlpha(0.0f);
                this.customView.setVisibility(View.INVISIBLE);
                return;
            }
            return;
        }
        this.tv_pager_indicator.setVisibility(View.INVISIBLE);
        this.img_close.setVisibility(View.INVISIBLE);
        this.img_save.setVisibility(View.INVISIBLE);
        this.pager.setVisibility(View.INVISIBLE);
        this.photoViewContainer.isReleasing = true;
        this.snapshotView.setVisibility(View.VISIBLE);
        this.snapshotView.post(new Runnable() {
            /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass3 */

            public void run() {
                if (Build.VERSION.SDK_INT >= 26) {
                    TransitionManager.beginDelayedTransition((ViewGroup) ImageViewerPopupView.this.snapshotView.getParent(), new TransitionSet().setDuration((long) ImageViewerPopupView.this.getAnimationDuration()).addTransition(new ChangeBounds()).addTransition(new ChangeTransform()).addTransition(new ChangeImageTransform()).setInterpolator((TimeInterpolator) new FastOutSlowInInterpolator()).addListener((Transition.TransitionListener) new TransitionListenerAdapter() {
                        /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass3.AnonymousClass1 */

                        public void onTransitionStart(Transition transition) {
                            super.onTransitionStart(transition);
                            ImageViewerPopupView.this.doAfterDismiss();
                        }

                        public void onTransitionEnd(Transition transition) {
                            ImageViewerPopupView.this.pager.setScaleX(1.0f);
                            ImageViewerPopupView.this.pager.setScaleY(1.0f);
                            ImageViewerPopupView.this.snapshotView.setScaleX(1.0f);
                            ImageViewerPopupView.this.snapshotView.setScaleY(1.0f);
                            ImageViewerPopupView.this.placeholderView.setVisibility(View.INVISIBLE);
                            ImageViewerPopupView.this.snapshotView.setTranslationX((float) ImageViewerPopupView.this.rect.left);
                            ImageViewerPopupView.this.snapshotView.setTranslationY((float) ImageViewerPopupView.this.rect.top);
                            XPopupUtils.setWidthHeight(ImageViewerPopupView.this.snapshotView, ImageViewerPopupView.this.rect.width(), ImageViewerPopupView.this.rect.height());
                        }
                    }));
                }
                ImageViewerPopupView.this.snapshotView.setScaleX(1.0f);
                ImageViewerPopupView.this.snapshotView.setScaleY(1.0f);
                ImageViewerPopupView.this.snapshotView.setTranslationX((float) ImageViewerPopupView.this.rect.left);
                ImageViewerPopupView.this.snapshotView.setTranslationY((float) ImageViewerPopupView.this.rect.top);
                ImageViewerPopupView.this.snapshotView.setScaleType(ImageViewerPopupView.this.srcView.getScaleType());
                XPopupUtils.setWidthHeight(ImageViewerPopupView.this.snapshotView, ImageViewerPopupView.this.rect.width(), ImageViewerPopupView.this.rect.height());
                ImageViewerPopupView.this.animateShadowBg(0);
                if (ImageViewerPopupView.this.customView != null) {
                    ImageViewerPopupView.this.customView.animate().alpha(0.0f).setDuration((long) ImageViewerPopupView.this.getAnimationDuration()).setListener(new AnimatorListenerAdapter() {
                        /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass3.AnonymousClass2 */

                        public void onAnimationEnd(Animator animator) {
                            super.onAnimationEnd(animator);
                            if (ImageViewerPopupView.this.customView != null) {
                                ImageViewerPopupView.this.customView.setVisibility(View.INVISIBLE);
                            }
                        }
                    }).start();
                }
            }
        });
    }

    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public void dismiss() {
        if (this.popupStatus == PopupStatus.Show) {
            this.popupStatus = PopupStatus.Dismissing;
            doDismissAnimation();
        }
    }

    public ImageViewerPopupView setImageUrls(List<Object> list) {
        this.urls = list;
        return this;
    }

    public ImageViewerPopupView setSrcViewUpdateListener(OnSrcViewUpdateListener onSrcViewUpdateListener) {
        this.srcViewUpdateListener = onSrcViewUpdateListener;
        return this;
    }

    public ImageViewerPopupView setXPopupImageLoader(XPopupImageLoader xPopupImageLoader) {
        this.imageLoader = xPopupImageLoader;
        return this;
    }

    public ImageViewerPopupView isShowPlaceholder(boolean z) {
        this.isShowPlaceholder = z;
        return this;
    }

    public ImageViewerPopupView isShowIndicator(boolean z) {
        this.isShowIndicator = z;
        return this;
    }

    public ImageViewerPopupView isShowSaveButton(boolean z) {
        this.isShowSaveBtn = z;
        return this;
    }

    public ImageViewerPopupView isInfinite(boolean z) {
        this.isInfinite = z;
        return this;
    }

    public ImageViewerPopupView setPlaceholderColor(int i) {
        this.placeholderColor = i;
        return this;
    }

    public ImageViewerPopupView setPlaceholderRadius(int i) {
        this.placeholderRadius = i;
        return this;
    }

    public ImageViewerPopupView setPlaceholderStrokeColor(int i) {
        this.placeholderStrokeColor = i;
        return this;
    }

    public ImageViewerPopupView setBgColor(int i) {
        this.bgColor = i;
        return this;
    }

    public ImageViewerPopupView setLongPressListener(OnImageViewerLongPressListener onImageViewerLongPressListener) {
        this.longPressListener = onImageViewerLongPressListener;
        return this;
    }

    public ImageViewerPopupView setSingleSrcView(ImageView imageView, Object obj) {
        if (this.urls == null) {
            this.urls = new ArrayList();
        }
        this.urls.clear();
        this.urls.add(obj);
        setSrcView(imageView, 0);
        return this;
    }

    public ImageViewerPopupView setSrcView(ImageView imageView, int i) {
        this.srcView = imageView;
        this.position = i;
        if (imageView != null) {
            int[] iArr = new int[2];
            imageView.getLocationInWindow(iArr);
            int activityContentLeft = iArr[0] - getActivityContentLeft();
            if (XPopupUtils.isLayoutRtl(getContext())) {
                int i2 = -((XPopupUtils.getAppWidth(getContext()) - iArr[0]) - imageView.getWidth());
                this.rect = new Rect(i2, iArr[1], imageView.getWidth() + i2, iArr[1] + imageView.getHeight());
            } else {
                this.rect = new Rect(activityContentLeft, iArr[1], imageView.getWidth() + activityContentLeft, iArr[1] + imageView.getHeight());
            }
        }
        return this;
    }

    public void updateSrcView(ImageView imageView) {
        setSrcView(imageView, this.position);
        addOrUpdateSnapshot();
    }

    @Override // com.youyi.yyviewsdklibrary.Dialog.interfaces.OnDragChangeListener
    public void onRelease() {
        dismiss();
    }

    @Override // com.youyi.yyviewsdklibrary.Dialog.interfaces.OnDragChangeListener
    public void onDragChange(int i, float f, float f2) {
        float f3 = 1.0f - f2;
        this.tv_pager_indicator.setAlpha(f3);
        View view = this.customView;
        if (view != null) {
            view.setAlpha(f3);
        }
        ImageView imageView = this.img_close;
        if (imageView != null) {
            imageView.setAlpha(f3);
        }
        if (this.isShowSaveBtn) {
            this.img_save.setAlpha(f3);
        }
        this.photoViewContainer.setBackgroundColor(((Integer) this.argbEvaluator.evaluate(f2 * 0.8f, Integer.valueOf(this.bgColor), 0)).intValue());
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public void onDismiss() {
        super.onDismiss();
        this.srcView = null;
        this.srcViewUpdateListener = null;
    }

    public void onClick(View view) {
        if (view == this.img_save) {
            save();
        }
        if (view == this.img_close) {
            dismiss();
        }
    }

    @Override // com.youyi.yyviewsdklibrary.Dialog.core.BasePopupView
    public void destroy() {
        super.destroy();
        HackyViewPager hackyViewPager = this.pager;
        hackyViewPager.removeOnPageChangeListener((PhotoViewAdapter) hackyViewPager.getAdapter());
        this.imageLoader = null;
    }

    /* access modifiers changed from: protected */
    public int getRealPosition() {
        return this.isInfinite ? this.position % this.urls.size() : this.position;
    }

    /* access modifiers changed from: protected */
    public void save() {
        XPermission.create(getContext(), PermissionConstants.STORAGE).callback(new XPermission.SimpleCallback() {
            /* class com.youyi.yyviewsdklibrary.Dialog.core.ImageViewerPopupView.AnonymousClass4 */

            @Override // com.youyi.yyviewsdklibrary.Dialog.util.XPermission.SimpleCallback
            public void onDenied() {
            }

            @Override // com.youyi.yyviewsdklibrary.Dialog.util.XPermission.SimpleCallback
            public void onGranted() {
                XPopupUtils.saveBmpToAlbum(ImageViewerPopupView.this.getContext(), ImageViewerPopupView.this.imageLoader, ImageViewerPopupView.this.urls.get(ImageViewerPopupView.this.getRealPosition()));
            }
        }).request();
    }

    public class PhotoViewAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
        @Override // android.support.v4.view.PagerAdapter
        public boolean isViewFromObject(View view, Object obj) {
            return obj == view;
        }

        @Override // android.support.v4.view.ViewPager.OnPageChangeListener
        public void onPageScrollStateChanged(int i) {
        }

        @Override // android.support.v4.view.ViewPager.OnPageChangeListener
        public void onPageScrolled(int i, float f, int i2) {
        }

        public PhotoViewAdapter() {
        }

        @Override // android.support.v4.view.PagerAdapter
        public int getCount() {
            if (ImageViewerPopupView.this.isInfinite) {
                return 100000;
            }
            return ImageViewerPopupView.this.urls.size();
        }

        @Override // android.support.v4.view.PagerAdapter
        public Object instantiateItem(ViewGroup viewGroup, int i) {
            if (ImageViewerPopupView.this.isInfinite) {
                i %= ImageViewerPopupView.this.urls.size();
            }
            FrameLayout buildContainer = buildContainer(viewGroup.getContext());
            ProgressBar buildProgressBar = buildProgressBar(viewGroup.getContext());
            XPopupImageLoader xPopupImageLoader = ImageViewerPopupView.this.imageLoader;
            Object obj = ImageViewerPopupView.this.urls.get(i);
            ImageViewerPopupView imageViewerPopupView = ImageViewerPopupView.this;
            buildContainer.addView(xPopupImageLoader.loadImage(i, obj, imageViewerPopupView, imageViewerPopupView.snapshotView, buildProgressBar), new LayoutParams(-1, -1));
            buildContainer.addView(buildProgressBar);
            viewGroup.addView(buildContainer);
            return buildContainer;
        }

        private FrameLayout buildContainer(Context context) {
            FrameLayout frameLayout = new FrameLayout(context);
            frameLayout.setLayoutParams(new LayoutParams(-1, -1));
            return frameLayout;
        }

        private ProgressBar buildProgressBar(Context context) {
            ProgressBar progressBar = new ProgressBar(context);
            progressBar.setIndeterminate(true);
            int dp2px = XPopupUtils.dp2px(ImageViewerPopupView.this.container.getContext(), 40.0f);
            LayoutParams layoutParams = new LayoutParams(dp2px, dp2px);
            layoutParams.gravity = 17;
            progressBar.setLayoutParams(layoutParams);
            progressBar.setVisibility(View.GONE);
            return progressBar;
        }

        @Override // android.support.v4.view.PagerAdapter
        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            viewGroup.removeView((View) obj);
        }

        @Override // android.support.v4.view.ViewPager.OnPageChangeListener
        public void onPageSelected(int i) {
            ImageViewerPopupView.this.position = i;
            ImageViewerPopupView.this.showPagerIndicator();
            if (ImageViewerPopupView.this.srcViewUpdateListener != null) {
                OnSrcViewUpdateListener onSrcViewUpdateListener = ImageViewerPopupView.this.srcViewUpdateListener;
                ImageViewerPopupView imageViewerPopupView = ImageViewerPopupView.this;
                onSrcViewUpdateListener.onSrcViewUpdate(imageViewerPopupView, imageViewerPopupView.getRealPosition());
            }
        }
    }
}
